<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>07-动画2</title>
  <style>
    body {
      background-color: #000;
      text-align: center;
    }

    #c1 {
      background-color: #fff;
    }
  </style>
</head>

<body>
  <canvas id="c1" width="800" height="600"></canvas>

  <script>
    window.onload = function () {
      const oC = document.getElementById('c1')
      const gd = oC.getContext('2d')
      let left = 100

      // 请求一个动画帧
      requestAnimationFrame(next)

      function next(){
        gd.clearRect(0, 0, oC.width, oC.height)
        left += 5
        gd.strokeRect(left, 100, 100, 50)

        // 让它循环请求
        requestAnimationFrame(next)
      }
    }
  </script>
</body>

</html>